package com.tm.controller;

import com.tm.mapper.StudentMapper;
import com.tm.pojo.entity.Student;
import com.tm.pojo.vo.StudentVo;
import com.tm.service.StudentService;
import com.tm.until.Result;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @program: springboot02
 * @description:
 * @author: 小仝
 * @create: 2023-04-12 19:40
 **/

@RestController
@RequestMapping("student")
@Api(tags = "学生接口类")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Resource
    private StudentMapper studentMapper;
    @PostMapping("findAll/{current}/{pageSize}")
    @ApiOperation("分页条件查询所有")
    public Result findAll(
            @ApiParam(value = "当前页面",name ="current",required = true,defaultValue = "1")
            @PathVariable Integer current ,  //设置默认值
            @ApiParam(value = "每页条数",name ="pageSize",required = true,defaultValue = "5")
            @PathVariable Integer pageSize,
            @RequestBody StudentVo StudentVo
    ){

        System.out.println(current);

        com.tm.until.Result all = studentService.findAll(current, pageSize, StudentVo);
        com.tm.until.Result result = new com.tm.until.Result(200, "查询成功", all);
        return result;
    }

    /**
     * 添加数据
     */
    @PutMapping("saveStudent")
    @ApiOperation("添加数据")
    public Result saveStudent(@RequestBody Student student){
        int insert = studentMapper.insert(student);
        return  new Result(200,"添加成功",insert);
    }

    /**
     * 删除数据
     */
    @DeleteMapping("deleteStudent/{sid}")
    @ApiOperation("删除数据")
    public Result deleteStudent(@PathVariable Integer sid){
        int i = studentMapper.deleteById(sid);
        Result result = new Result(200, "删除成功", i);
        return result;

    }


    /**
     * 修改数据
     */

    @PutMapping("updateStudent")
    @ApiOperation("修改数据")
    public Result updateStudent(@RequestBody Student student){
        int i = studentMapper.updateById(student);
        return new Result(200,"修改成功",i);
    }

    /**
     * 根据ID查询 数据
     */
    @PostMapping("findById/{sid}")
    @ApiOperation("根据ID查询")
    public Result findById(@PathVariable Integer sid){
        Student student = studentMapper.selectById(sid);
        return new Result(200,"查询成功",student);

    }
}
